package com.edz.zxpmifiapk;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.io.InputStreamReader;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn_startService)
    Button btn_startService;
    @BindView(R.id.btn_stopService)
    Button btn_stopService;
    @BindView(R.id.tv_receiveData)
    TextView tv_receiveData;

    @BindView(R.id.btn_data2trim)
    Button btn_data2trim;
    @BindView(R.id.tv_displaydata)
    TextView tv_displaydata;
    @BindView(R.id.btn_shutdownBroadCast)
    Button btn_shutdownBroadCast;
    @BindView(R.id.btn_testTimer)
    Button btn_testTimer;

    private IntentFilter intentFilter;
    private ShutDownBroadcastReceiver shutDownBroadcastReceiver;
    private static final String ACTION_SHUTDOWN = "android.intent.action.ACTION_SHUTDOWN";
    private static final String url             = "https://res.v5fans.com/yidao/start.html?bid=2631";
    private static final String TAG             = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        redirectWeb(url);
    }

    private void redirectWeb(String url) {
        Log.i(TAG,"----开始网络重定向----");
    }

    /**
     * 启动服务
     */
    @OnClick({R.id.btn_startService})
    void setBtn_startService(){
        Intent startService = new Intent(this,TestService.class);
        startService(startService);
        showToast("-----服务已经开启-----");
    }

    /**
     * 断开socket连接
     */
    @OnClick({R.id.btn_stopService})
    void setBtn_stopService(){
        Intent stopService = new Intent(this,TestService.class);
        stopService(stopService);
    }

    /**
     * 测试发送的数据
     */
    @OnClick({R.id.btn_data2trim})
    void testDataChange(){
        String strReq = "{\"MsgType\":\"WebRedirectCtl\";\"CmdType\":\"EnableRedirect\";\"WebRedirectUrl\":\"http://www.hilan.com.cn/\";\"AcceptIPList\":\"121.199.50.33\"}";
        tv_displaydata.setText(strReq.getBytes()+"");
    }

    /**
     * 测试关机广播
     */
    @OnClick({R.id.btn_shutdownBroadCast})
    void testShutdownBroadcast(){
        //动态注册广播接收器
        intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_SHUTDOWN);
        shutDownBroadcastReceiver = new ShutDownBroadcastReceiver();
        registerReceiver(shutDownBroadcastReceiver,intentFilter);
        showToast("关机广播已经动态注册");
    }

    @OnClick({R.id.btn_testTimer})
    void testTimer(){
        SocketUtil.getInstance().sendHeartData("test data");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(shutDownBroadcastReceiver);
    }

    class ShutDownBroadcastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(ACTION_SHUTDOWN)) {
                showToast("接收到开机广播");
                Log.e("text_guangb", "jieshoudao guanji  guangbi");
            }
        }
    }

    private void showToast(String msg){
        Toast toast = Toast.makeText(this,msg,Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}
